package cn.cai.pethome.pet.service.impl;

import cn.cai.pethome.pet.domain.Image;
import cn.cai.pethome.pet.domain.Pet;
import cn.cai.pethome.pet.domain.PetDetail;
import cn.cai.pethome.pet.domain.PetOnlineAuditLog;
import cn.cai.pethome.pet.dto.PetOnlineAuditDto;
import cn.cai.pethome.pet.mapper.*;
import cn.cai.pethome.pet.service.IPetService;
import cn.cai.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaocai
 * @since 2022-09-10
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    /**
     * 批量保存宠物图片
     * @param pet
     */
    @Transactional
    @Override
    public void add(Pet pet) {
        //先保存宠物主表信息，返回主键
        super.add(pet);

        //保存宠物详情信息t_pet_detail表
        PetDetail petDetail = new PetDetail();
        petDetail.setPet_id(pet.getId());
        petDetail.setAdoptNotice(pet.getAdoptNotice());  //领养须知
        petDetail.setIntro(pet.getIntro());    //简介
        petDetailMapper.add(petDetail);

        //批量保存宠物图片t_image表
        if (pet.getImgUrls() != null && pet.getImgUrls().size() > 0){
            imageMapper.batchSave(pet.getImgUrls(),1,pet.getId());
        }
    }

    @Override
    public void update(Pet pet) {
        if (pet.getImgUrls() != null && pet.getImgUrls().size() > 0){
            pet.setPic(pet.getImgUrls().get(0));//获取t_pet表宠物表的第一张图片添加进去
        }
        super.update(pet);

        //在修改t_pet_detail的图片地址adoptNotice，inter 根据petid修改
        petDetailMapper.updateByPetId(pet.getAdoptNotice(),pet.getIntro(),pet.getId());

        //在修改t_image表之前先根据petid删除数据在添加数据
        imageMapper.deleteByBusinessId(pet.getId());
        //再批量保存宠物图片t_image表
        if (pet.getImgUrls() != null && pet.getImgUrls().size() > 0){
            imageMapper.batchSave(pet.getImgUrls(),1,pet.getId());
        }


    }

    /**
     * 根据宠物id查询宠物的图片集合地址和宠物详情数据
     * @param peiId
     * @return
     */
    @Override
    public Map<String, Object> getIMagesAndDetail(Long peiId) {
        //查询t_image表的宠物图片
        List<Image> images = imageMapper.findByTypeAndBusinessId(1,peiId);

        //查询t_pet_detail表的领养通知，和简介
        PetDetail petDetail = petDetailMapper.findByPetId(peiId);

        //用map封装起来
        Map<String, Object> map = new HashMap<>();
        map.put("images",images);
        map.put("petDetail",petDetail);
        return map;
    }

    /**
     * 宠物上架申请店铺员工使用
     * @param petIds
     * @return
     */
    @Transactional
    @Override
    public void onsale(List<Long> petIds) {

        //判断您是否勾选要上架的宠物没
        Assert.isTrue(petIds != null && petIds.size() >0,"要上架的宠物不存在！");

        //根据petIds查询数据t_pet表
        List<Pet> petList = petMapper.loadBatchIds(petIds);
        //筛选集合是否有包含已经上架的宠物
        List<Pet> onlinedList = petList.stream().filter(pet -> pet.getState() == 1).collect(Collectors.toList());
        Assert.isTrue(onlinedList.isEmpty(),"您勾选的宠物中包含了已经上架的宠物！");

        //批量修改宠物状态 把t_pet表state状态修改为2
        petMapper.updateBatchState(petIds, 2);

        //批量插入上架申请记录  把t_pet_online_audit_log表的申请记录添加进去
        petOnlineAuditLogMapper.saveBatch(petIds);


    }

    /**
     * 提交宠物上架审核结果
     * @param petOnlineAuditDto
     * @return
     */
    @Override
    public void petOnlineApplyAudit(PetOnlineAuditDto petOnlineAuditDto) {
        if(petOnlineAuditDto.getAuditResult() == 1){
            //审核通过：宠物上架 保存审核日志
            List<Long> petIds = new ArrayList<>();
            petIds.add(petOnlineAuditDto.getPet_id());
            petMapper.updateBatchState(petIds, 1);

            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setPet_id(petOnlineAuditDto.getPet_id());
            petOnlineAuditLog.setState(1);
            petOnlineAuditLog.setAudit_time(new Date());
            petOnlineAuditLog.setNote("审核通过");
            petOnlineAuditLog.setAudit_id(petOnlineAuditDto.getLogininfoId());
            petOnlineAuditLogMapper.updateByPetId(petOnlineAuditLog);
        }else{
            //拒绝：宠物下架 保存审核日志 发送邮件
            List<Long> petIds = new ArrayList<>();
            petIds.add(petOnlineAuditDto.getPet_id());
            petMapper.updateBatchState(petIds, 0);

            PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
            petOnlineAuditLog.setPet_id(petOnlineAuditDto.getPet_id());
            petOnlineAuditLog.setState(2);
            petOnlineAuditLog.setAudit_time(new Date());
            petOnlineAuditLog.setNote(petOnlineAuditDto.getRejectReason());
            petOnlineAuditLog.setAudit_id(petOnlineAuditDto.getLogininfoId());
            petOnlineAuditLogMapper.updateByPetId(petOnlineAuditLog);
        }
    }

    /**
     * 根据宠物id查询宠物的图片集合和宠物详情数据
     * @param petId
     * @return
     */
    @Override
    public Map<String, Object> loadPetDetail(Long petId) {
        //1.查询宠物主表数据，及店铺消息  t_pet表的数据
        Pet pet = petMapper.findById(petId);

        //2.根据宠物id查询宠物详情及详情数据
        Map<String, Object> petDetailMap = this.getIMagesAndDetail(petId);

        //封装到map中传入前端
        petDetailMap.put("pet",pet);

        return petDetailMap;
    }
}
